//
//  CollectCaseVC.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/9.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "CollectCaseVC.h"
#import "CaseBaseModel.h"
#import "CollectCaseVCTableViewCell.h"
@interface CollectCaseVC ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation CollectCaseVC

-(void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTable];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self creatrfromSQL];
    [_tableView reloadData];
}
-(void)creatrfromSQL
{
    self.dataArr = [[DataBaseManager shareInstance ] selectAll];
    //NSLog(@"%@",self.dataArr);
}
-(void)createTable
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 200;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[CollectCaseVCTableViewCell class] forCellReuseIdentifier:@"CollectCaseVCcell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArr.count == 0) {
        return 1;
    }else{
        return self.dataArr.count;
    }
   // return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectCaseVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectCaseVCcell" forIndexPath:indexPath];
    if (_dataArr.count == 0) {
        cell.title.text = @"暂无收藏,请先收藏";
        cell.imgView.image = [UIImage imageNamed:@"24099F061BD92F37796E9B75C3ED9531.jpg"];
    }else{
        CaseBaseModel *m = _dataArr[indexPath.row];
        NSLog(@"%@",m.coverPhoto);
        cell.title.text = m.title;
        NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w750_h398_m0.jpg",m.coverPhoto];
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    }
    
    return cell;
}
//编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_dataArr.count > 1) {
        // 删除数据库
        CaseBaseModel *m = _dataArr[indexPath.row];
        [[DataBaseManager shareInstance] deleteBytitle:m.title];
        // 删除数组元素
        [_dataArr removeObjectAtIndex:indexPath.row];
        // 删除cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
    } else if (_dataArr.count == 1) {
        // 删除数据库
        CaseBaseModel *m = _dataArr[indexPath.row];
        [[DataBaseManager shareInstance] deleteBytitle:m.title];
        // 删除数组元素
        [_dataArr removeObjectAtIndex:indexPath.row];
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
